{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sentence Reversal\n",
    "\n",
    "## Problem\n",
    "\n",
    "Given a string of words, reverse all the words. For example:\n",
    "\n",
    "Given:\n",
    "    \n",
    "    'This is the best'\n",
    "\n",
    "Return:\n",
    "\n",
    "    'best the is This'\n",
    "\n",
    "As part of this exercise you should remove all leading and trailing whitespace. So that inputs such as:\n",
    "\n",
    "    '  space here'  and 'space here      '\n",
    "\n",
    "both become:\n",
    "\n",
    "    'here space'\n",
    "\n",
    "## Solution\n",
    "\n",
    "We could take advantage of Python's abilities and solve the problem with the use of **split()** and some slicing or use of **reversed**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def rev_word1(s):\n",
    "    return \" \".join(reversed(s.split()))\n",
    "\n",
    "#Or\n",
    "\n",
    "def rev_word2(s):\n",
    "    return \" \".join(s.split()[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'go? to ready you are John, Hi'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word1('Hi John,   are you ready to go?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'go? to ready you are John, Hi'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word2('Hi John,   are you ready to go?')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While these are valid solutions, in an interview setting you'll have to work out the basic algorithm that is used. In this case what we want to do is loop over the text and extract words form the string ourselves. Then we can push the words to a \"stack\" and in the end opo them all to reverse. Let's see what this actually looks like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def rev_word3(s):\n",
    "    \"\"\"\n",
    "    Manually doing the splits on the spaces.\n",
    "    \"\"\"\n",
    "    \n",
    "    words = []\n",
    "    length = len(s)\n",
    "    spaces = [' ']\n",
    "    \n",
    "    # Index Tracker\n",
    "    i = 0\n",
    "    \n",
    "    # While index is less than length of string\n",
    "    while i < length:\n",
    "        \n",
    "        # If element isn't a space\n",
    "        if s[i] not in spaces:\n",
    "            \n",
    "            # The word starts at this index\n",
    "            word_start = i\n",
    "            \n",
    "            while i < length and s[i] not in spaces:\n",
    "                \n",
    "                # Get index where word ends\n",
    "                i += 1\n",
    "            # Append that word to the list\n",
    "            words.append(s[word_start:i])\n",
    "        # Add to index\n",
    "        i += 1\n",
    "        \n",
    "    # Join the reversed words\n",
    "    return \" \".join(reversed(words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'you are how John Hello'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word3('   Hello John    how are you   ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'before space'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word3('    space before')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want you can further develop this solution so its all manual, you can create your own reversal function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Your Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ALL TEST CASES PASSED\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "RUN THIS CELL TO TEST YOUR SOLUTION\n",
    "\"\"\"\n",
    "\n",
    "from nose.tools import assert_equal\n",
    "\n",
    "class ReversalTest(object):\n",
    "    \n",
    "    def test(self,sol):\n",
    "        assert_equal(sol('    space before'),'before space')\n",
    "        assert_equal(sol('space after     '),'after space')\n",
    "        assert_equal(sol('   Hello John    how are you   '),'you are how John Hello')\n",
    "        assert_equal(sol('1'),'1')\n",
    "        print \"ALL TEST CASES PASSED\"\n",
    "        \n",
    "# Run and test\n",
    "t = ReversalTest()\n",
    "t.test(rev_word)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Good Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
